package com.smarttiger.zero.admin.task;

import com.smarttiger.zero.admin.dao.TaskDao;
import com.smarttiger.zero.admin.dao.TaskLogicDao;
import com.smarttiger.zero.admin.entity.LogicModel;
import com.smarttiger.zero.admin.entity.TaskModel;
import com.smarttiger.zero.admin.util.Message;
import com.smarttiger.zero.logic.ExecuteLogics;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by zhouzhengwei on 2021/11/13.
 */
@RestController
@RequestMapping("/model/quartz")
public class QuartzApiController {

    @Autowired
    TaskDao taskDao;
    @Autowired
    private QuartzScheduler quartzScheduler;

    @Autowired
    TaskLogicDao taskLogicDao;


    @RequestMapping("/start/{code}")
    public Message startQuartzJob(@PathVariable String code) {
        Message message = new Message();
        try {
            TaskModel t = taskDao.findByCode(code);
            if("R".equals(t.getStatus())){
                quartzScheduler.deleteJob(code);
            }
            if(taskLogicDao.getLogicByTaskId(t.getId()).size()==0){
                message.setData("logic没有配置");
                message.setCode("0");
                return message;
            }
            quartzScheduler.startJob(t);
        } catch (SchedulerException e) {
            e.printStackTrace();
            message.setCode("0");
        }
        return message;
    }

    @RequestMapping("/info/{code}")
    public String getQuartzJob(@PathVariable  String code) {
        String info = null;
        try {
            info = quartzScheduler.getJobInfo(code);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return info;
    }

    @RequestMapping("/modify/{code}/{cron}")
    public boolean modifyQuartzJob(
            @PathVariable  String code,
            @PathVariable  String cron) {
        boolean flag = true;
        try {
            flag = quartzScheduler.modifyJob(code, cron);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @RequestMapping(value = "/pause/{code}")
    public void pauseQuartzJob(@PathVariable String code) {
        try {
            quartzScheduler.pauseJob(code);
            TaskModel t = taskDao.findByCode(code);
            t.setStatus("P");
            taskDao.save(t);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/pauseAll")
    public void pauseAllQuartzJob() {
        try {
            quartzScheduler.pauseAllJob();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/delete/{code}")
    public Message deleteJob(@PathVariable String code) {
        Message message = new Message();
        try {
            quartzScheduler.deleteJob(code);
        } catch (SchedulerException e) {
            e.printStackTrace();
            message.setCode("0");
        }
        return message;
    }


    @RequestMapping(value = "/manual/{code}")
    public Message manualJob(@PathVariable String code) {
        Message message = new Message();
        try {
            TaskModel t = taskDao.findByCode(code);
            String taskId = t.getId();
            String taskName = t.getName();
            System.out.println("手动执行："+taskName);
            List<LogicModel> logics = taskLogicDao.getLogicByTaskId(taskId);
            if(logics.size()==0){
                message.setCode("0");
                message.setData("逻辑块没有配置");
            }else {
                try {
                    ExecuteLogics.executeLogics(logics);
                    message.setCode("1");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("手动执行："+taskName+"结束");

        } catch (Exception e) {
            e.printStackTrace();
            message.setCode("0");
        }
        return message;
    }

}

